﻿using glTech.Log4netWrapper;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;

namespace FutianCloudAPIService.Domains
{
    /// <summary>
    /// 识别类,包含所有识别的功能代码.
    /// </summary>
    public class RecognizeDomain
    {
        private Winstone.WinStoneApi _winStoneApi;
        public RecognizeDomain(Winstone.WinStoneApi winStoneApi)
        {
            _winStoneApi = winStoneApi;
        }

        /// <summary>
        /// 通过请求的Files和Image,返回名片的文字json
        /// </summary>
        /// <param name="repFiles">请求的文件</param>
        /// <param name="repImage">请求的imageurl</param>
        /// <returns>名片的json格式</returns>
        internal async System.Threading.Tasks.Task<string> RecogCard(IEnumerable<Stream> repFiles, string repImage)
        {
            string json = "";
            try
            {
                if (repFiles.Any())
                {
                    var file = repFiles.First();
                    json = await _winStoneApi.Recognize(file);

                    Logger.LogInfo("recog card as attachment");
                }
                else
                {
                    //下载文件
                    var webClient = new WebClient();
                    using (var stream = webClient.OpenRead(repImage))
                    {
                        json = await _winStoneApi.Recognize(stream);
                    }
                }
            }
            catch (Exception ex )
            {
                Logger.LogError("RecognizeDomain->RecogCard-> Recognize Server error:" + ex);
                return "recognize server error";
            }

            try
            {
                ResponseJson rep = JsonConvert.DeserializeObject<ResponseJson>(json);
                if (rep.Message.Status == 0 && rep.Cardsinfo.Any() && rep.Cardsinfo[0].Type == 20)
                {
                    //识别成功, type=20 是名片
                    Logger.LogInfo("recognize sucess, raw json is:"+json);
                    var finalCardJson = JsonConvert.SerializeObject(new FinalCard(rep));
                    Logger.LogInfo("final card json is:" + finalCardJson);
                    return finalCardJson;
                }

                Logger.LogInfo("card can't be recognized because "+rep.Message.value);
                return "card can't be recognized";
            }
            catch (Exception ex)
            {
                Logger.LogError("RecognizeDomain->RecogCard-> Serialize json error:" + ex);
                return "recognize serialize error";
            }
        }
    }

    public class FinalCard
    {
        public FinalCard(ResponseJson r)
        {
            var c = r.Cardsinfo.First();
            foreach (var item in c.Items)
            {
                var d = item.Desc.Trim();
                switch (d)
                {
                    case "姓名":
                        UserXM = item.Content;
                        break;
                    case "职务/部门":
                        Department = item.Content;
                        break;
                    case "手机":
                        TelePhone = item.Content;
                        break;
                    case "公司":
                        Company = item.Content;
                        break;
                    case "地址":
                        Address = item.Content;
                        break;
                    case "电话":
                        MobilePhone = item.Content;
                        break;
                    case "传真":
                        FixNo = item.Content;
                        break;
                    case "电子邮箱":
                        EMail = item.Content;
                        break;
                    case "网址":
                        WebSite = item.Content;
                        break;
                    case "邮编":
                        PostCode = item.Content;
                        break;
                }
            }
        }

        public string UserXM { get; set; }
        public string Department { get; set; }
        public string TelePhone { get; set; }
        public string Company { get; set; }
        public string Address { get; set; }
        public string MobilePhone { get; set; }
        public string FixNo { get; set; }
        public string EMail { get; set; }
        public string WebSite { get; set; }
        public string PostCode { get; set; }
    }

    public class ResponseMessage
    {
        public int Status { get; set; }
        public string value { get; set; }
    }

    public class ResponseJson
    {
        public ResponseMessage Message { get; set; }

        public List<CardInfo> Cardsinfo { get; set; }
    }

    public class CardInfo
    {
        public int Type { get; set; }

        public List<CardItem> Items { get; set; }
    }

    public class CardItem
    {
        public string Desc { get; set; }
        public string Content { get; set; }
    }
}
